package com.yellowpage.tool;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by Sun on 2015/10/29.
 */
public class Test {


    // @param filePath 原始图片路径
    // @param watermarkPath 水印图片路径
    //@param watermarkX 水印位置:x
    // @param watermarkY 水印位置:y
    // @param watermarkAlpha 透明度
    public static  void main(String args[]) throws IOException
    {

        UserGet.getUserList(2,2);

        FileInputStream fis = new FileInputStream("resource/table.ini");

        //暂存的水印地址
        File watermarkFileName = new File(System.getProperty("user.dir")
                + "/" + System.currentTimeMillis() + ".png");


        int width = 500; // 水印图片的宽度
        int height = 200;

        // 创建一个和原图片同大小的新空白图片,得到画笔对象g  给出一个支持透明的格式
        BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);

        Graphics2D g = bimage.createGraphics();

        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));


        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);//填充整个屏幕




        // 设置字体
        Font font = new Font("SansSerif", Font.BOLD, 100);
        g.setFont(font);
        // 设置字体颜色
        g.setColor(Color.red);

        // @param watermarkAlpha 透明度表示字体透明 0最小 1最大
        float watermarkAlpha =  0.15f;
        //值从0f-1.0f
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, watermarkAlpha));
        // 画字两边 两个数字表示字体位置
        g.drawString("制作水印", width / 10, height / 2);

        //设置前景透明结束
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        for (int j1 = bimage.getMinY(); j1 < bimage
                .getHeight(); j1++) {
            for (int j2 = bimage.getMinX(); j2 < bimage
                    .getWidth(); j2++) {
                int rgb = bimage.getRGB(j2, j1);

                int R =(rgb & 0xff0000 ) >> 16 ;
                int G= (rgb & 0xff00 ) >> 8 ;
                int B= (rgb & 0xff );
                if(((255-R)<30) && ((255-G)<30) && ((255-B)<30)){
                    rgb = ((0 + 1) << 24) | (rgb & 0x00ffffff);
                }

                bimage.setRGB(j2, j1, rgb);

            }
        }

        g.dispose();
        FileOutputStream out = null;
        try {


            out = new FileOutputStream(watermarkFileName);

            ImageIO.write(bimage, "png", out);


            //设置图片质量
            //param.setQuality(100f, true);
            //encoder.encode(bimage, param);
        } catch (Exception e) {
            System.out.println("---生成失败---");

        } finally {
            if (out != null) {
                try {
                    out.close();
                    out = null;
                } catch (Exception e) {
                }
            }
        }
        System.out.println("===生成成功===");

    }


    public byte[] transferAlpha(Image image) {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {
            ImageIcon imageIcon = new ImageIcon(image);
            BufferedImage bufferedImage = new BufferedImage(imageIcon
                    .getIconWidth(), imageIcon.getIconHeight(),
                    BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
            g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon
                    .getImageObserver());
            int alpha = 0;
            for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage
                    .getHeight(); j1++) {
                for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage
                        .getWidth(); j2++) {
                    int rgb = bufferedImage.getRGB(j2, j1);

                    int R =(rgb & 0xff0000 ) >> 16 ;
                    int G= (rgb & 0xff00 ) >> 8 ;
                    int B= (rgb & 0xff );
                    if(((255-R)<30) && ((255-G)<30) && ((255-B)<30)){
                        rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
                    }

                    bufferedImage.setRGB(j2, j1, rgb);

                }
            }

            g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
            //ImageIO.write(bufferedImage, "png", new File("d:/test.png.png"));

            ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
        } catch (Exception e) {

        }finally{

        }

        return byteArrayOutputStream.toByteArray();

    }
}